#include "CudaProgram.h"

using namespace OBR;

std::vector<OptixProgramGroup> CudaProgram::all_program_groups;

CudaProgram::CudaProgram(OptixDeviceContext &ctx,
                         OptixPipelineCompileOptions &pipeline_compile_options,
                         OptixProgramGroupOptions &prog_group_options,
                         OptixProgramGroupDesc &prog_group_desc)
{
    // create
    char *log = new char[MAX_INTERNAL_LOG_SIZE];
    size_t log_size = MAX_INTERNAL_LOG_SIZE;
    OPTIX_CHECK(optixProgramGroupCreate(ctx, &prog_group_desc, 1, &prog_group_options, log,
                                        &log_size, &this->program_group);
                std::cout << "=============== Program Group Create Log ==============="
                          << std::endl;
                std::cout << log << std::endl; delete[] log;);

    // push to all programs
    this->program_id = CudaProgram::all_program_groups.size();
    CudaProgram::all_program_groups.push_back(this->program_group);
}

CudaProgram::~CudaProgram()
{
    OPTIX_CHECK_NOEXCEPT(optixProgramGroupDestroy(this->program_group));
}